package com.android.lovepick;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.lovepick.http.HttpThread;
import com.android.lovepick.util.BaseUtils;
import com.android.lovepick.util.Constant;

public class TestMsgActivity extends Activity
{
	private final String TESTMSG_TAG = "TestMsgActivity";
	private ProgressDialog mDialog = null;
	private boolean isSendMsg = false;
	private boolean isRcvMsg = false;
	private String message = "";

	private TextView txvMsgSend;
	private TextView txvMsgRcv;

	private HttpThread mThread = null;

	@Override
	public void onCreate(Bundle bundle)
	{
		super.onCreate(bundle);
		setContentView(R.layout.test_msg);

		txvMsgSend = (TextView) this.findViewById(R.id.txv_msg_send);
		txvMsgRcv = (TextView) this.findViewById(R.id.txv_msg_rcv);
		mDialog = new ProgressDialog(this);
	}

	public void sendMsg(View v)
	{
		Log.i(TESTMSG_TAG, "发送消息");
		isSendMsg = true;
		String url = Constant.HTTP_SEND_MSG_URL;
		url += "1";
		String _r = String.valueOf(Math.random());
		url += "&_r=" + _r;
		List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
		message = "这是一条用来测试的消息!";
		paramsList.add(new BasicNameValuePair("message", message));
		mThread = new HttpThread(mHandler, Constant.HTTP_METHOD_POST, url,
				paramsList);
		mThread.start();
		mDialog.setTitle("发送消息");
		mDialog.setMessage("消息发送中...");
		mDialog.show();
	}

	public void receiveMsg(View v)
	{
		Log.i(TESTMSG_TAG, "接收消息");
		isRcvMsg = true;
		String url = Constant.HTTP_GET_NEW_MSG_URL;
		String _r = String.valueOf(Math.random());
		url += "&_r=" + _r;
		url += "&LastID=0";
		mThread = new HttpThread(mHandler, Constant.HTTP_METHOD_GET, url, null);
		mThread.start();
		mDialog.setTitle("接收消息");
		mDialog.setMessage("消息接收中...");
		mDialog.show();
	}

	private Handler mHandler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case Constant.MSG_HTTP_SUCCESS:
					String result = msg.getData().getString("http_result");
					// Log.i(TESTMSG_TAG, result);
					mDialog.dismiss();

					if (isSendMsg)
					{
						txvMsgSend.setText(message);
						BaseUtils.showToast(TestMsgActivity.this, "消息发送成功!");
						Log.i(TESTMSG_TAG, "消息发送成功!");
						isSendMsg = false;
					}
					else if (isRcvMsg)
					{

						try
						{
							JSONObject jsonObject = new JSONObject(result);
							String messages = jsonObject.getJSONArray(
									"messages").toString();
							txvMsgRcv.setText(messages);
							Log.i(TESTMSG_TAG, "接收消息成功!");
						}
						catch (JSONException e)
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

						isRcvMsg = false;
					}

					break;

				case Constant.MSG_HTTP_ERROR:
					Log.i(TESTMSG_TAG, "http error...");
					mDialog.dismiss();
					break;

				default:
					break;
			}
		}
	};

}
